package com.example.dqhperformance.service.impl;

import com.example.dqhperformance.service.CreateHorizontalScorerService;
import com.example.dqhperformance.utils.DingUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CreateHorizontalScorerServiceImpl implements CreateHorizontalScorerService {

    @Autowired
    DingUtil dingUtil;

    @Override
    public Integer createHorizontalScorer(String id) {
        try {
            //得到项目表数据
            JSONObject projectJson = new JSONObject(dingUtil.getYidaById(id));
            if (projectJson.has("tableField_lz9pjb5f")) {
                JSONArray memberArray = projectJson.getJSONArray("tableField_lz9pjb5f");
                Map<String, List<String>> memberAndPositionMap = new HashMap();
                memberArray.forEach(item -> {
                    JSONObject memberJson = (JSONObject) item;
                    if (memberJson.has("selectField_m0i1jp0u")) {
                        String type = memberJson.getString("selectField_m0i1jp0u");
                        String member = memberJson.getString("selectField_lz9pjb5g");
                        if (type.equals("部员")) {
                        } else {
                            if (memberAndPositionMap.get(type) != null) {
                                List<String> mermberList = memberAndPositionMap.get(type);
                                mermberList.add(member);
                                memberAndPositionMap.put(type, mermberList);
                            } else {
                                List<String> memberList = new ArrayList<>();
                                memberList.add(member);
                                memberAndPositionMap.put(type, memberList);
                            }
                        }
                    }
                });
                JSONObject upJson = new JSONObject();
                upJson.put("textField_m0g2sks0", projectJson.getString("textField_lz9pjb4v"));//项目名称
                memberArray.forEach(item -> {
                    JSONObject memberJson = (JSONObject) item;
                    String type = memberJson.getString("selectField_lz6g9umf");
                    List<String> member = (List<String>) memberJson.get("employeeField_m0kgrs1r");//被考核人
                    upJson.put("employeeField_m0ki23ap", member);
                    switch (type) {
                        case "部员":
                            int score_by1 = 20;
                            int score_by2 = 50;
                            int score_by3 = 30;
                            //找副部长-部长-分管
                            if (memberAndPositionMap.get("项目所属部门副职") != null) {
                                upJson.put("employeeField_m0i36dwu", memberAndPositionMap.get("项目所属部门副职"));//打分人1
                                upJson.put("numberField_m0g2sks6", score_by1);
                            } else {
                                score_by2 = score_by2 + score_by1;
                            }
                            if (memberAndPositionMap.get("项目所属部门正职") != null) {
                                upJson.put("employeeField_m0i36dwv", memberAndPositionMap.get("项目所属部门正职"));//打分人2
                                upJson.put("numberField_m0g2sks7", score_by2);
                            } else {
                                score_by3 = score_by3 + score_by2;
                            }
                            if (memberAndPositionMap.get("项目分管领导") != null) {
                                upJson.put("employeeField_m0i36dww", memberAndPositionMap.get("项目分管领导"));//打分人3
                                upJson.put("numberField_m0g2sks8", score_by3);
                            }
                            break;
                        case "项目所属部门副职":
                            int score_fz1 = 60;
                            int score_fz2 = 40;
                            int score_fz3 = 0;
                            //找部长-分管
                            if (memberAndPositionMap.get("项目所属部门正职") != null) {
                                upJson.put("employeeField_m0i36dwu", memberAndPositionMap.get("项目所属部门正职"));//打分人1
                                upJson.put("numberField_m0g2sks6", score_fz1);
                            } else {
                                score_fz2 = score_fz2 + score_fz1;
                            }
                            if (memberAndPositionMap.get("项目分管领导") != null) {
                                upJson.put("employeeField_m0i36dwv", memberAndPositionMap.get("项目分管领导"));//打分人2
                                upJson.put("numberField_m0g2sks7", score_fz2);
                            } else {
                                score_fz3 = score_fz3 + score_fz2;
                            }
                            if (score_fz3 != 0) {
                                upJson.put("employeeField_m0i36dwv", memberAndPositionMap.get("项目经理"));//打分人2
                                upJson.put("numberField_m0g2sks7", score_fz2 * 0.6);
                                upJson.put("employeeField_m0i36dww", memberAndPositionMap.get("项目党支部书记"));//打分人3
                                upJson.put("numberField_m0g2sks8", score_fz3 * 0.4);
                            }
                            break;
                        case "项目所属部门正职":
                            int score_zz1 = 40;
                            int score_zz2 = 60;
                            int score_zz3 = 0;
                            if (memberAndPositionMap.get("项目分管领导") != null) {
                                upJson.put("employeeField_m0i36dwu", memberAndPositionMap.get("项目分管领导"));//打分人1
                                upJson.put("numberField_m0g2sks6", score_zz1);
                            } else {
                                score_zz2 = score_zz2 + score_zz1;
                            }
                            if (memberAndPositionMap.get("项目主管领导") != null) {
                                upJson.put("employeeField_m0i36dwv", memberAndPositionMap.get("项目经理"));//打分人2
                                upJson.put("numberField_m0g2sks7", score_zz2 * 0.6);
                                upJson.put("employeeField_m0i36dww", memberAndPositionMap.get("项目党支部书记"));//打分人3
                                upJson.put("numberField_m0g2sks8", score_zz3 * 0.4);
                            }
                            break;
                        default:
                            break;
                    }
                    System.out.println(upJson);
                });
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}